/*!
  @file graphcut.cpp
  @copyright 2013 Kyohei Otsu. All rights resereved.
*/

#include <cstdio>
#include <cstdlib>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <boost/shared_ptr.hpp>

#include "kcv/stereo/stereo_gc.h"

using namespace std;
using namespace cv;

void usage()
{
  fprintf(stdout, "USAGE: ./graphcut <left_image> <right_image> <scale:1.0>\n");
}

int main(int ac, char **av)
{
  if (ac < 3)
  {
    usage();
    exit(0);
  }

  std::string pathL(av[1]);
  std::string pathR(av[2]);
  double scale = (ac >= 4)? atof(av[3]): 1.0;
  int min_disp = 0;
  int max_disp = 32;

  /* read images */
  cv::Mat imL = imread(pathL, 0);
  cv::Mat imR = imread(pathR, 0);
  cv::Mat disparity;
  if (!imL.data || !imR.data)
  {
    perror("No such images");
    exit(-1);
  }

  cv::imshow("L", imL);
  cv::waitKey(1);
  cv::imshow("R", imR);
  cv::waitKey(1);

  /* perform stereo matching */
  boost::shared_ptr<kcv::StereoGC> stereo(new kcv::StereoGC(kcv::StereoGC::DATA_COST_ZNCC));
  stereo->setScale(scale);
  stereo->setDisparityLimits(min_disp, max_disp);
  stereo->setWindowSize(5);
  //stereo->setDebug(true);
  stereo->process(imL, imR, disparity);

  /* show disparity map */
  stereo->show_disparity();
  cv::waitKey(-1);

  return 0;
}

